package Formularios;

import java.sql.ResultSet;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Thiago
 */
public class viewCadastroFunc extends javax.swing.JPanel {

    /**
     * Creates new form viewCadastroFunc
     */
    public viewCadastroFunc() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelEndereco = new java.awt.Label();
        labelSalario = new java.awt.Label();
        textFieldTelefone = new javax.swing.JFormattedTextField();
        textAreaEndereco = new java.awt.TextArea();
        textFieldCPF = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        textFieldCTPS = new javax.swing.JFormattedTextField();
        labelCTPS = new java.awt.Label();
        Limpar = new java.awt.Button();
        labelCPF = new java.awt.Label();
        jComboBox1 = new javax.swing.JComboBox();
        labelTelefone = new java.awt.Label();
        label1 = new java.awt.Label();
        textFieldNome = new java.awt.TextField();
        labelNome = new java.awt.Label();
        button1 = new java.awt.Button();
        textFieldSalario = new javax.swing.JFormattedTextField();

        setMaximumSize(new java.awt.Dimension(7366, 768));
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        labelEndereco.setText("Endereço:");

        labelSalario.setText("Salario:");

        try {
            textFieldTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) #### - ####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            textFieldCPF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel1.setText("Serviço:");

        try {
            textFieldCTPS.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##.###.#####/##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        labelCTPS.setText("CTPS");

        Limpar.setLabel("Limpar");
        Limpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LimparActionPerformed(evt);
            }
        });

        labelCPF.setText("CPF:");

        labelTelefone.setText("Telefone:");

        label1.setAlignment(java.awt.Label.CENTER);
        label1.setFont(new java.awt.Font("Dialog", 1, 18));
        label1.setText("Cadastro de Funcionários");

        labelNome.setText("Nome:");

        button1.setLabel("Incluir");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        textFieldSalario.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(101, 101, 101)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(labelCTPS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(labelEndereco, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jComboBox1, 0, 351, Short.MAX_VALUE)
                                .addGap(187, 187, 187))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(textFieldCTPS)
                                    .addComponent(textFieldTelefone, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
                                .addGap(374, 374, 374)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(labelSalario, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(128, 128, 128)
                                .addComponent(label1, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(textFieldCPF)
                            .addComponent(textFieldSalario, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE))
                        .addGap(183, 183, 183))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(519, 519, 519)
                        .addComponent(Limpar, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(25, 25, 25)
                        .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(textAreaEndereco, javax.swing.GroupLayout.DEFAULT_SIZE, 872, Short.MAX_VALUE)
                    .addComponent(textFieldNome, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 872, Short.MAX_VALUE))
                .addGap(22, 22, 22))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label1, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(textFieldNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(textFieldTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(8, 8, 8))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(8, 8, 8)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textFieldCPF, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelCTPS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textFieldCTPS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelSalario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textFieldSalario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelEndereco, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                        .addGap(52, 52, 52))
                    .addComponent(textAreaEndereco, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(62, 62, 62)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jComboBox1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(button1, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                    .addComponent(Limpar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(20, 20, 20))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void LimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LimparActionPerformed
        // TODO add your handling code here:
        limparCampos();

    }//GEN-LAST:event_LimparActionPerformed

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
        // TODO add your handling code here:
        String nome, telefone, cpf, ctps, salariot, endereco = null;
        String servico = null;
        int idServico = 0;
        float salario = 0;
        boolean existe;

        if (textFieldNome.getText().length() < 4 || textFieldNome.getText().length() > 60) {
            SalaoApp.mostraMensagem("Verifique o nome digitado, ele deve conter entre 4 e 60 caracteres!", "Cadastro Funcionario");
        } else if (textFieldCPF.getText().length() < 14 || textFieldCPF.getText().contains(" ") || textFieldCPF.getText().equals("   .   .   -  ")) {
            SalaoApp.mostraMensagem("Informe todos os número do CPF!", "Cadastro Funcionario");
        } else if (textFieldCTPS.getText().length() < 15 || textFieldCTPS.getText().contains(" ") || textFieldCTPS.getText().equals("  .   .     /  ")) {
            SalaoApp.mostraMensagem("CTPS deve conter todos os dígitos!", "Cadastro Funcionario");
        } else if (textFieldSalario.getText() == null || Integer.parseInt(textFieldSalario.getText()) < 0) {
            SalaoApp.mostraMensagem("Informe um salário!", "Cadastro Funcionario");
        } else if (textFieldSalario.getText().contains(".")) {
            SalaoApp.mostraMensagem("O salário não deve conter '.'! Informe o salário sem as casas decimais!", "Cadastro Funcionario");
        } else if (textFieldSalario.getText().length() > 4 || textFieldSalario.getText().length() == 0) {
            SalaoApp.mostraMensagem("O salário está incorreto!", "Cadastro Funcionario");
        } else if (textFieldTelefone.getText().length() < 15 || textFieldTelefone.getText().equals("(  )      -     ")) {
            SalaoApp.mostraMensagem("O Telefone deve ser preenchido por completo!", "Cadastro Funcionario");
        }else if(textAreaEndereco.getText().length() > 99){
             SalaoApp.mostraMensagem("O Endereço não pode ultrapassar os 99 caracteres!", "Cadastro Funcionario");
        }         else {
            nome = textFieldNome.getText();
            telefone = textFieldTelefone.getText();
            cpf = textFieldCPF.getText();
            ctps = textFieldCTPS.getText();
            salario = Float.parseFloat(textFieldSalario.getText());
            salariot = textFieldSalario.getText();
            endereco = textAreaEndereco.getText();
            servico = jComboBox1.getSelectedItem().toString();
            existe = SalaoApp.controle.existeFuncionario(cpf);
            if (existe) {
                SalaoApp.mostraMensagem("Funcionário já está cadastrado!\n Caso seja necessário, utilize a função de Alteração", "Cadastro Funcionario");
            } else {
                try {
                    ResultSet rs = SalaoApp.controle.consultaServico();

                    while (rs.next()) {
                        if (rs.getString(2).toString().equals(servico.toString())) {
                            idServico = Integer.parseInt(rs.getString(1));
                            break;
                        }
                    }

                } catch (Exception exception) {
                    exception.printStackTrace();
                    System.exit(1);
                }


                SalaoApp.controle.cadastrarFuncionario(nome, telefone, endereco, cpf, ctps, salario, idServico);
                LimparActionPerformed(evt);
                jComboBox1.setSelectedItem("");
            }
        }
    }//GEN-LAST:event_button1ActionPerformed

    private void limparCampos() {
        textAreaEndereco.setText("");
        textFieldCPF.setText("");
        textFieldCTPS.setText("");
        textFieldNome.setText("");
        textFieldSalario.setText("");
        textFieldTelefone.setText("");
    }

    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        // TODO add your handling code here:
        limparCampos();
        try {

            ResultSet rs = SalaoApp.controle.consultaServico();

            DefaultComboBoxModel model = new DefaultComboBoxModel();

            jComboBox1.setModel(model);

            while (rs.next()) {
                //String nomeTelefone = rs.getString(2) + " - " + rs.getString(3);
                jComboBox1.addItem(rs.getString(2));
            }
            rs.close();

        } catch (Exception exception) {
            exception.printStackTrace();
            System.exit(1);
        }
    }//GEN-LAST:event_formAncestorAdded
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button Limpar;
    private java.awt.Button button1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private java.awt.Label label1;
    private java.awt.Label labelCPF;
    private java.awt.Label labelCTPS;
    private java.awt.Label labelEndereco;
    private java.awt.Label labelNome;
    private java.awt.Label labelSalario;
    private java.awt.Label labelTelefone;
    private java.awt.TextArea textAreaEndereco;
    private javax.swing.JFormattedTextField textFieldCPF;
    private javax.swing.JFormattedTextField textFieldCTPS;
    private java.awt.TextField textFieldNome;
    private javax.swing.JFormattedTextField textFieldSalario;
    private javax.swing.JFormattedTextField textFieldTelefone;
    // End of variables declaration//GEN-END:variables
}
